home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxchar.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  40.2 KB  |  1,306 lines

  1. /* Copyright (C) 1989, 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxchar.c,v 1.2 2000/09/19 19:00:34 lpd Exp $ */
  20. /* Default implementation of text writing */
  21. #include "gx.h"
  22. #include "memory_.h"
  23. #include "string_.h"
  24. #include "gserrors.h"
  25. #include "gsstruct.h"
  26. #include "gxfixed.h"        /* ditto */
  27. #include "gxarith.h"
  28. #include "gxmatrix.h"
  29. #include "gzstate.h"
  30. #include "gxcoord.h"
  31. #include "gxdevice.h"
  32. #include "gxdevmem.h"
  33. #include "gxchar.h"
  34. #include "gxfont.h"
  35. #include "gxfont0.h"
  36. #include "gxfcache.h"
  37. #include "gspath.h"
  38. #include "gzpath.h"
  39.  
  40. /* Define whether or not to cache characters rotated by angles other than */
  41. /* multiples of 90 degrees. */
  42. private bool CACHE_ROTATED_CHARS = true;
  43.  
  44. /* Define whether or not to oversample characters at small sizes. */
  45. private bool OVERSAMPLE = true;
  46.  
  47. /* Define the maximum size of a full temporary bitmap when rasterizing, */
  48. /* in bits (not bytes). */
  49. private uint MAX_TEMP_BITMAP_BITS = 80000;
  50.  
  51. /* Define whether the show operation uses the character outline data, */
  52. /* as opposed to just needing the width (or nothing). */
  53. #define SHOW_USES_OUTLINE(penum)\
  54.   !SHOW_IS(penum, TEXT_DO_NONE | TEXT_DO_CHARWIDTH)
  55.  
  56. /* Structure descriptors */
  57. public_st_gs_show_enum();
  58. extern_st(st_gs_text_enum);
  59. extern_st(st_gs_state);        /* only for testing */
  60. private 
  61. ENUM_PTRS_BEGIN(show_enum_enum_ptrs)
  62.      return ENUM_USING(st_gs_text_enum, vptr, size, index);
  63. ENUM_PTR(0, gs_show_enum, pgs);
  64. ENUM_PTR(1, gs_show_enum, show_gstate);
  65. ENUM_PTR3(2, gs_show_enum, dev_cache, dev_cache2, dev_null);
  66. ENUM_PTRS_END
  67. private RELOC_PTRS_WITH(show_enum_reloc_ptrs, gs_show_enum *eptr)
  68. {
  69.     RELOC_USING(st_gs_text_enum, vptr, size);        /* superclass */
  70.     RELOC_VAR(eptr->pgs);
  71.     RELOC_VAR(eptr->show_gstate);
  72.     RELOC_PTR3(gs_show_enum, dev_cache, dev_cache2, dev_null);
  73. }
  74. RELOC_PTRS_END
  75.  
  76. /* Forward declarations */
  77. private int continue_kshow(P1(gs_show_enum *));
  78. private int continue_show(P1(gs_show_enum *));
  79. private int continue_show_update(P1(gs_show_enum *));
  80. private void show_set_scale(P1(gs_show_enum *));
  81. private int show_cache_setup(P1(gs_show_enum *));
  82. private int show_state_setup(P1(gs_show_enum *));
  83. private int show_origin_setup(P4(gs_state *, fixed, fixed, gs_char_path_mode));
  84.  
  85. /* Accessors for current_char and current_glyph. */
  86. #define CURRENT_CHAR(penum) ((penum)->returned.current_char)
  87. #define SET_CURRENT_CHAR(penum, chr)\
  88.   ((penum)->returned.current_char = (chr))
  89. #define CURRENT_GLYPH(penum) ((penum)->returned.current_glyph)
  90. #define SET_CURRENT_GLYPH(penum, glyph)\
  91.   ((penum)->returned.current_glyph = (glyph))
  92.  
  93. /* Allocate a show enumerator. */
  94. gs_show_enum *
  95. gs_show_enum_alloc(gs_memory_t * mem, gs_state * pgs, client_name_t cname)
  96. {
  97.     gs_show_enum *penum;
  98.  
  99.     rc_alloc_struct_1(penum, gs_show_enum, &st_gs_show_enum, mem,
  100.               return 0, cname);
  101.     penum->rc.free = rc_free_text_enum;
  102.     penum->auto_release = true;    /* old API */
  103.     /* Initialize pointers for GC */
  104.     penum->text.operation = 0;    /* no pointers relevant */
  105.     penum->dev = 0;
  106.     penum->pgs = pgs;
  107.     penum->show_gstate = 0;
  108.     penum->dev_cache = 0;
  109.     penum->dev_cache2 = 0;
  110.     penum->dev_null = 0;
  111.     penum->fstack.depth = -1;
  112.     return penum;
  113. }
  114.  
  115. /* ------ Driver procedure ------ */
  116.  
  117. private text_enum_proc_resync(gx_show_text_resync);
  118. private text_enum_proc_process(gx_show_text_process);
  119. private text_enum_proc_is_width_only(gx_show_text_is_width_only);
  120. private text_enum_proc_current_width(gx_show_text_current_width);
  121. private text_enum_proc_set_cache(gx_show_text_set_cache);
  122. private text_enum_proc_retry(gx_show_text_retry);
  123. private text_enum_proc_release(gx_show_text_release); /* not default */
  124.  
  125. private const gs_text_enum_procs_t default_text_procs = {
  126.     gx_show_text_resync, gx_show_text_process,
  127.     gx_show_text_is_width_only, gx_show_text_current_width,
  128.     gx_show_text_set_cache, gx_show_text_retry,
  129.     gx_show_text_release
  130. };
  131.  
  132. int
  133. gx_default_text_begin(gx_device * dev, gs_imager_state * pis,
  134.               const gs_text_params_t * text, gs_font * font,
  135.               gx_path * path, const gx_device_color * pdcolor,
  136.               const gx_clip_path * pcpath,
  137.               gs_memory_t * mem, gs_text_enum_t ** ppte)
  138. {
  139.     uint operation = text->operation;
  140.     bool propagate_charpath = (operation & TEXT_DO_DRAW) != 0;
  141.     int code;
  142.     gs_state *pgs = (gs_state *)pis;
  143.     gs_show_enum *penum;
  144.  
  145.     /*
  146.      * For the moment, require pis to be a gs_state *, since all the
  147.      * procedures for character rendering expect it.
  148.      */
  149.     if (gs_object_type(mem, pis) != &st_gs_state)
  150.     return_error(gs_error_Fatal);
  151.     penum = gs_show_enum_alloc(mem, pgs, "gx_default_text_begin");
  152.     if (!penum)
  153.     return_error(gs_error_VMerror);
  154.     code = gs_text_enum_init((gs_text_enum_t *)penum, &default_text_procs,
  155.                  dev, pis, text, font, path, pdcolor, pcpath, mem);
  156.     if (code < 0) {
  157.     gs_free_object(mem, penum, "gx_default_text_begin");
  158.     return code;
  159.     }
  160.     penum->auto_release = false; /* new API */
  161.     penum->level = pgs->level;
  162.     if (operation & TEXT_DO_ANY_CHARPATH)
  163.     penum->charpath_flag =
  164.         (operation & TEXT_DO_FALSE_CHARPATH ? cpm_false_charpath :
  165.          operation & TEXT_DO_TRUE_CHARPATH ? cpm_true_charpath :
  166.          operation & TEXT_DO_FALSE_CHARBOXPATH ? cpm_false_charboxpath :
  167.          operation & TEXT_DO_TRUE_CHARBOXPATH ? cpm_true_charboxpath :
  168.          operation & TEXT_DO_CHARWIDTH ? cpm_charwidth :
  169.          cpm_show /* can't happen */ );
  170.     else
  171.     penum->charpath_flag =
  172.         (propagate_charpath ? pgs->in_charpath : cpm_show);
  173.     penum->cc = 0;
  174.     penum->continue_proc = continue_show;
  175.     /* Note: show_state_setup may reset can_cache. */
  176.     switch (penum->charpath_flag) {
  177.     case cpm_false_charpath: case cpm_true_charpath:
  178.     penum->can_cache = -1; break;
  179.     case cpm_false_charboxpath: case cpm_true_charboxpath:
  180.     penum->can_cache = 0; break;
  181.     case cpm_charwidth:
  182.     default:            /* cpm_show */
  183.     penum->can_cache = 1; break;
  184.     }
  185.     code = show_state_setup(penum);
  186.     if (code < 0)
  187.     return code;
  188.     penum->show_gstate =
  189.     (propagate_charpath && (pgs->in_charpath != 0) ?
  190.      pgs->show_gstate : pgs);
  191.     if (!(~operation & (TEXT_DO_NONE | TEXT_RETURN_WIDTH))) {
  192.     /* This is stringwidth. */
  193.     gx_device_null *dev_null =
  194.         gs_alloc_struct(mem, gx_device_null, &st_device_null,
  195.                 "stringwidth(dev_null)");
  196.  
  197.     if (dev_null == 0)
  198.         return_error(gs_error_VMerror);
  199.     /* Do an extra gsave and suppress output */
  200.     if ((code = gs_gsave(pgs)) < 0)
  201.         return code;
  202.     penum->level = pgs->level;    /* for level check in show_update */
  203.     /* Set up a null device that forwards xfont requests properly. */
  204.     gs_make_null_device(dev_null, gs_currentdevice_inline(pgs), mem);
  205.     pgs->ctm_default_set = false;
  206.     penum->dev_null = dev_null;
  207.     /* Retain this device, since it is referenced from the enumerator. */
  208.     gx_device_retain((gx_device *)dev_null, true);
  209.     gs_setdevice_no_init(pgs, (gx_device *) dev_null);
  210.     /* Establish an arbitrary translation and current point. */
  211.     gs_newpath(pgs);
  212.     gx_translate_to_fixed(pgs, fixed_0, fixed_0);
  213.     code = gx_path_add_point(pgs->path, fixed_0, fixed_0);
  214.     if (code < 0)
  215.         return code;
  216.     }
  217.     *ppte = (gs_text_enum_t *)penum;
  218.     return 0;
  219. }
  220.  
  221. /* ------ Width/cache setting ------ */
  222.  
  223. private int
  224.     set_char_width(P4(gs_show_enum *penum, gs_state *pgs,
  225.               floatp wx, floatp wy)),
  226.     set_cache_device(P6(gs_show_enum *penum, gs_state *pgs,
  227.             floatp llx, floatp lly, floatp urx, floatp ury));
  228.  
  229. /* This is the default implementation of text enumerator set_cache. */
  230. private int
  231. gx_show_text_set_cache(gs_text_enum_t *pte, const double *pw,
  232.               gs_text_cache_control_t control)
  233. {
  234.     gs_show_enum *const penum = (gs_show_enum *)pte;
  235.     gs_state *pgs = penum->pgs;
  236.  
  237.     switch (control) {
  238.     case TEXT_SET_CHAR_WIDTH:
  239.     return set_char_width(penum, pgs, pw[0], pw[1]);
  240.     case TEXT_SET_CACHE_DEVICE: {
  241.     int code = set_char_width(penum, pgs, pw[0], pw[1]);    /* default is don't cache */
  242.  
  243.     if (code < 0)
  244.         return code;
  245.     return set_cache_device(penum, pgs, pw[2], pw[3], pw[4], pw[5]);
  246.     }
  247.     case TEXT_SET_CACHE_DEVICE2: {
  248.     int code;
  249.  
  250.     if (gs_rootfont(pgs)->WMode) {
  251.         float vx = pw[8], vy = pw[9];
  252.         gs_fixed_point pvxy, dvxy;
  253.         cached_char *cc;
  254.  
  255.         if ((code = gs_point_transform2fixed(&pgs->ctm, -vx, -vy, &pvxy)) < 0 ||
  256.         (code = gs_distance_transform2fixed(&pgs->ctm, vx, vy, &dvxy)) < 0
  257.         )
  258.         return 0;        /* don't cache */
  259.         if ((code = set_char_width(penum, pgs, pw[6], pw[7])) < 0)
  260.         return code;
  261.         /* Adjust the origin by (vx, vy). */
  262.         gx_translate_to_fixed(pgs, pvxy.x, pvxy.y);
  263.         code = set_cache_device(penum, pgs, pw[2], pw[3], pw[4], pw[5]);
  264.         if (code != 1)
  265.         return code;
  266.         /* Adjust the character origin too. */
  267.         cc = penum->cc;
  268.         cc->offset.x += dvxy.x;
  269.         cc->offset.y += dvxy.y;
  270.     } else {
  271.         code = set_char_width(penum, pgs, pw[0], pw[1]);
  272.         if (code < 0)
  273.         return code;
  274.         code = set_cache_device(penum, pgs, pw[2], pw[3], pw[4], pw[5]);
  275.     }
  276.     return code;
  277.     }
  278.     default:
  279.     return_error(gs_error_rangecheck);
  280.     }
  281. }
  282.  
  283. /* Set the character width. */
  284. /* Note that this returns 1 if the current show operation is */
  285. /* non-displaying (stringwidth or cshow). */
  286. private int
  287. set_char_width(gs_show_enum *penum, gs_state *pgs, floatp wx, floatp wy)
  288. {
  289.     int code;
  290.  
  291.     if (penum->width_status != sws_none)
  292.     return_error(gs_error_undefined);
  293.     if ((code = gs_distance_transform2fixed(&pgs->ctm, wx, wy, &penum->wxy)) < 0)
  294.     return code;
  295.     /* Check whether we're setting the scalable width */
  296.     /* for a cached xfont character. */
  297.     if (penum->cc != 0) {
  298.     penum->cc->wxy = penum->wxy;
  299.     penum->width_status = sws_cache_width_only;
  300.     } else {
  301.     penum->width_status = sws_no_cache;
  302.     }
  303.     return !SHOW_IS_DRAWING(penum);
  304. }
  305.  
  306. /* Set up the cache device if relevant. */
  307. /* Return 1 if we just set up a cache device. */
  308. /* Used by setcachedevice and setcachedevice2. */
  309. private int
  310. set_cache_device(gs_show_enum * penum, gs_state * pgs, floatp llx, floatp lly,
  311.          floatp urx, floatp ury)
  312. {
  313.     gs_glyph glyph;
  314.  
  315.     /* See if we want to cache this character. */
  316.     if (pgs->in_cachedevice)    /* no recursion! */
  317.     return 0;
  318.     pgs->in_cachedevice = CACHE_DEVICE_NOT_CACHING;    /* disable color/gray/image operators */
  319.     /* We can only use the cache if we know the glyph. */
  320.     glyph = CURRENT_GLYPH(penum);
  321.     if (glyph == gs_no_glyph)
  322.     return 0;
  323.     /* We can only use the cache if ctm is unchanged */
  324.     /* (aside from a possible translation). */
  325.     if (penum->can_cache <= 0 || !pgs->char_tm_valid) {
  326.     if_debug2('k', "[k]no cache: can_cache=%d, char_tm_valid=%d\n",
  327.           penum->can_cache, (int)pgs->char_tm_valid);
  328.     return 0;
  329.     } {
  330.     const gs_font *pfont = pgs->font;
  331.     gs_font_dir *dir = pfont->dir;
  332.     gx_device *dev = gs_currentdevice_inline(pgs);
  333.     int alpha_bits =
  334.     (*dev_proc(dev, get_alpha_bits)) (dev, go_text);
  335.     gs_log2_scale_point log2_scale;
  336.     static const fixed max_cdim[3] =
  337.     {
  338. #define max_cd(n)\
  339.         (fixed_1 << (arch_sizeof_short * 8 - n)) - (fixed_1 >> n) * 3
  340.         max_cd(0), max_cd(1), max_cd(2)
  341. #undef max_cd
  342.     };
  343.     ushort iwidth, iheight;
  344.     cached_char *cc;
  345.     gs_fixed_rect clip_box;
  346.     int code;
  347.  
  348.     /* Compute the bounding box of the transformed character. */
  349.     /* Since we accept arbitrary transformations, the extrema */
  350.     /* may occur in any order; however, we can save some work */
  351.     /* by observing that opposite corners before transforming */
  352.     /* are still opposite afterwards. */
  353.     gs_fixed_point cll, clr, cul, cur, cdim;
  354.  
  355.     if ((code = gs_distance_transform2fixed(&pgs->ctm, llx, lly, &cll)) < 0 ||
  356.         (code = gs_distance_transform2fixed(&pgs->ctm, llx, ury, &clr)) < 0 ||
  357.         (code = gs_distance_transform2fixed(&pgs->ctm, urx, lly, &cul)) < 0 ||
  358.      (code = gs_distance_transform2fixed(&pgs->ctm, urx, ury, &cur)) < 0
  359.         )
  360.         return 0;        /* don't cache */
  361.     {
  362.         fixed ctemp;
  363.  
  364. #define swap(a, b) ctemp = a, a = b, b = ctemp
  365. #define make_min(a, b) if ( (a) > (b) ) swap(a, b)
  366.  
  367.         make_min(cll.x, cur.x);
  368.         make_min(cll.y, cur.y);
  369.         make_min(clr.x, cul.x);
  370.         make_min(clr.y, cul.y);
  371. #undef make_min
  372. #undef swap
  373.     }
  374.     /* Now take advantage of symmetry. */
  375.     if (clr.x < cll.x)
  376.         cll.x = clr.x, cur.x = cul.x;
  377.     if (clr.y < cll.y)
  378.         cll.y = clr.y, cur.y = cul.y;
  379.     /* Now cll and cur are the extrema of the box. */
  380.     cdim.x = cur.x - cll.x;
  381.     cdim.y = cur.y - cll.y;
  382.     show_set_scale(penum);
  383.     log2_scale.x = penum->log2_suggested_scale.x;
  384.     log2_scale.y = penum->log2_suggested_scale.y;
  385. #ifdef DEBUG
  386.     if (gs_debug_c('k')) {
  387.         dlprintf6("[k]cbox=[%g %g %g %g] scale=%dx%d\n",
  388.               fixed2float(cll.x), fixed2float(cll.y),
  389.               fixed2float(cur.x), fixed2float(cur.y),
  390.               1 << log2_scale.x, 1 << log2_scale.y);
  391.         dlprintf6("[p]  ctm=[%g %g %g %g %g %g]\n",
  392.               pgs->ctm.xx, pgs->ctm.xy, pgs->ctm.yx, pgs->ctm.yy,
  393.               pgs->ctm.tx, pgs->ctm.ty);
  394.     }
  395. #endif
  396.     /*
  397.      * If the device wants anti-aliased text,
  398.      * increase the sampling scale to ensure that
  399.      * if we want N bits of alpha, we generate
  400.      * at least 2^N sampled bits per pixel.
  401.      */
  402.     if (alpha_bits > 1) {
  403.         int more_bits =
  404.         alpha_bits - (log2_scale.x + log2_scale.y);
  405.  
  406.         if (more_bits > 0) {
  407.         if (log2_scale.x <= log2_scale.y) {
  408.             log2_scale.x += (more_bits + 1) >> 1;
  409.             log2_scale.y += more_bits >> 1;
  410.         } else {
  411.             log2_scale.x += more_bits >> 1;
  412.             log2_scale.y += (more_bits + 1) >> 1;
  413.         }
  414.         }
  415.     } else if (!OVERSAMPLE || pfont->PaintType != 0) {
  416.         /* Don't oversample artificially stroked fonts. */
  417.         log2_scale.x = log2_scale.y = 0;
  418.     }
  419.     if (cdim.x > max_cdim[log2_scale.x] ||
  420.         cdim.y > max_cdim[log2_scale.y]
  421.         )
  422.         return 0;        /* much too big */
  423.     iwidth = ((ushort) fixed2int_var(cdim.x) + 2) << log2_scale.x;
  424.     iheight = ((ushort) fixed2int_var(cdim.y) + 2) << log2_scale.y;
  425.     if_debug3('k', "[k]iwidth=%u iheight=%u dev_cache %s\n",
  426.           (uint) iwidth, (uint) iheight,
  427.           (penum->dev_cache == 0 ? "not set" : "set"));
  428.     if (penum->dev_cache == 0) {
  429.         code = show_cache_setup(penum);
  430.         if (code < 0)
  431.         return code;
  432.     }
  433.     /*
  434.      * If we're oversampling (i.e., the temporary bitmap is
  435.      * larger than the final monobit or alpha array) and the
  436.      * temporary bitmap is large, use incremental conversion
  437.      * from oversampled bitmap strips to alpha values instead of
  438.      * full oversampling with compression at the end.
  439.      */
  440.     cc = gx_alloc_char_bits(dir, penum->dev_cache,
  441.                 (iwidth > MAX_TEMP_BITMAP_BITS / iheight &&
  442.                  log2_scale.x + log2_scale.y > alpha_bits ?
  443.                  penum->dev_cache2 : NULL),
  444.                 iwidth, iheight, &log2_scale, alpha_bits);
  445.     if (cc == 0)
  446.         return 0;        /* too big for cache */
  447.     /* The mins handle transposed coordinate systems.... */
  448.     /* Truncate the offsets to avoid artifacts later. */
  449.     cc->offset.x = fixed_ceiling(-cll.x);
  450.     cc->offset.y = fixed_ceiling(-cll.y);
  451.     if_debug4('k', "[k]width=%u, height=%u, offset=[%g %g]\n",
  452.           (uint) iwidth, (uint) iheight,
  453.           fixed2float(cc->offset.x),
  454.           fixed2float(cc->offset.y));
  455.     if ((code = gs_gsave(pgs)) < 0) {
  456.         gx_free_cached_char(dir, cc);
  457.         return code;
  458.     }
  459.     /* Nothing can go wrong now.... */
  460.     penum->cc = cc;
  461.     cc->code = glyph;
  462.     cc->wmode = gs_rootfont(pgs)->WMode;
  463.     cc->wxy = penum->wxy;
  464.     /* Install the device */
  465.     gx_set_device_only(pgs, (gx_device *) penum->dev_cache);
  466.     pgs->ctm_default_set = false;
  467.     /* Adjust the transformation in the graphics context */
  468.     /* so that the character lines up with the cache. */
  469.     gx_translate_to_fixed(pgs,
  470.                   cc->offset.x << log2_scale.x,
  471.                   cc->offset.y << log2_scale.y);
  472.     if ((log2_scale.x | log2_scale.y) != 0)
  473.         gx_scale_char_matrix(pgs, 1 << log2_scale.x,
  474.                  1 << log2_scale.y);
  475.     /* Set the initial matrix for the cache device. */
  476.     penum->dev_cache->initial_matrix = ctm_only(pgs);
  477.     /* Set the oversampling factor. */
  478.     penum->log2_scale.x = log2_scale.x;
  479.     penum->log2_scale.y = log2_scale.y;
  480.     /* Reset the clipping path to match the metrics. */
  481.     clip_box.p.x = clip_box.p.y = 0;
  482.     clip_box.q.x = int2fixed(iwidth);
  483.     clip_box.q.y = int2fixed(iheight);
  484.     if ((code = gx_clip_to_rectangle(pgs, &clip_box)) < 0)
  485.         return code;
  486.     gx_set_device_color_1(pgs);    /* write 1's */
  487.     pgs->in_cachedevice = CACHE_DEVICE_CACHING;
  488.     }
  489.     penum->width_status = sws_cache;
  490.     return 1;
  491. }
  492.  
  493. /* Return the cache device status. */
  494. gs_in_cache_device_t
  495. gs_incachedevice(const gs_state *pgs)
  496. {
  497.     return pgs->in_cachedevice;
  498. }
  499.  
  500. /* ------ Enumerator ------ */
  501.  
  502. /*
  503.  * Set the encode_char procedure in an enumerator.
  504.  */
  505. private void
  506. show_set_encode_char(gs_show_enum * penum)
  507. {
  508.     penum->encode_char =
  509.     (SHOW_IS(penum, TEXT_FROM_GLYPHS | TEXT_FROM_SINGLE_GLYPH) ?
  510.      gs_no_encode_char :
  511.      gs_show_current_font(penum)->procs.encode_char);
  512. }
  513.  
  514. /*
  515.  * Resync a text operation with a different set of parameters.
  516.  * Currently this is implemented only for changing the data source.
  517.  */
  518. private int
  519. gx_show_text_resync(gs_text_enum_t *pte, const gs_text_enum_t *pfrom)
  520. {
  521.     gs_show_enum *const penum = (gs_show_enum *)pte;
  522.     int old_index = pte->index;
  523.  
  524.     if ((pte->text.operation ^ pfrom->text.operation) & ~TEXT_FROM_ANY)
  525.     return_error(gs_error_rangecheck);
  526.     pte->text = pfrom->text;
  527.     if (pte->index == old_index) {
  528.     show_set_encode_char(penum);
  529.     return 0;
  530.     } else
  531.     return show_state_setup(penum);
  532. }
  533.  
  534. /* Do the next step of a show (or stringwidth) operation */
  535. private int
  536. gx_show_text_process(gs_text_enum_t *pte)
  537. {
  538.     gs_show_enum *const penum = (gs_show_enum *)pte;
  539.  
  540.     return (*penum->continue_proc)(penum);
  541. }
  542.  
  543. /* Continuation procedures */
  544. private int show_update(P1(gs_show_enum * penum));
  545. private int show_move(P1(gs_show_enum * penum));
  546. private int show_proceed(P1(gs_show_enum * penum));
  547. private int show_finish(P1(gs_show_enum * penum));
  548. private int
  549. continue_show_update(gs_show_enum * penum)
  550. {
  551.     int code = show_update(penum);
  552.  
  553.     if (code < 0)
  554.     return code;
  555.     code = show_move(penum);
  556.     if (code != 0)
  557.     return code;
  558.     return show_proceed(penum);
  559. }
  560. private int
  561. continue_show(gs_show_enum * penum)
  562. {
  563.     return show_proceed(penum);
  564. }
  565. /* For kshow, the CTM or font may have changed, so we have to reestablish */
  566. /* the cached values in the enumerator. */
  567. private int
  568. continue_kshow(gs_show_enum * penum)
  569. {
  570.     int code = show_state_setup(penum);
  571.  
  572.     if (code < 0)
  573.     return code;
  574.     return show_proceed(penum);
  575. }
  576.  
  577. /* Update position */
  578. private int
  579. show_update(gs_show_enum * penum)
  580. {
  581.     gs_state *pgs = penum->pgs;
  582.     cached_char *cc = penum->cc;
  583.     int code;
  584.  
  585.     /* Update position for last character */
  586.     switch (penum->width_status) {
  587.     case sws_none:
  588.         /* Adobe interpreters assume a character width of 0, */
  589.         /* even though the documentation says this is an error.... */
  590.         penum->wxy.x = penum->wxy.y = 0;
  591.         break;
  592.     case sws_cache:
  593.         /* Finish installing the cache entry. */
  594.         /* If the BuildChar/BuildGlyph procedure did a save and a */
  595.         /* restore, it already undid the gsave in setcachedevice. */
  596.         /* We have to check for this by comparing levels. */
  597.         switch (pgs->level - penum->level) {
  598.         default:
  599.             return_error(gs_error_invalidfont);        /* WRONG */
  600.         case 2:
  601.             code = gs_grestore(pgs);
  602.             if (code < 0)
  603.             return code;
  604.         case 1:
  605.             ;
  606.         }
  607.         gx_add_cached_char(pgs->font->dir, penum->dev_cache,
  608.                    cc, gx_lookup_fm_pair(pgs->font, pgs),
  609.                    &penum->log2_scale);
  610.         if (!SHOW_USES_OUTLINE(penum) ||
  611.         penum->charpath_flag != cpm_show
  612.         )
  613.         break;
  614.         /* falls through */
  615.     case sws_cache_width_only:
  616.         /* Copy the bits to the real output device. */
  617.         code = gs_grestore(pgs);
  618.         if (code < 0)
  619.         return code;
  620.         code = gs_state_color_load(pgs);
  621.         if (code < 0)
  622.         return code;
  623.         return gx_image_cached_char(penum, cc);
  624.     case sws_no_cache:
  625.         ;
  626.     }
  627.     if (penum->charpath_flag != cpm_show) {
  628.     /* Move back to the character origin, so that */
  629.     /* show_move will get us to the right place. */
  630.     code = gx_path_add_point(pgs->show_gstate->path,
  631.                  penum->origin.x, penum->origin.y);
  632.     if (code < 0)
  633.         return code;
  634.     }
  635.     return gs_grestore(pgs);
  636. }
  637.  
  638. /* Move to next character */
  639. private int
  640. show_fast_move(gs_state * pgs, gs_fixed_point * pwxy)
  641. {
  642.     int code = gx_path_add_rel_point_inline(pgs->path, pwxy->x, pwxy->y);
  643.  
  644.     /* If the current position is out of range, don't try to move. */
  645.     if (code == gs_error_limitcheck && pgs->clamp_coordinates)
  646.     code = 0;
  647.     return code;
  648. }
  649. private int
  650. show_move(gs_show_enum * penum)
  651. {
  652.     gs_state *pgs = penum->pgs;
  653.  
  654.     if (SHOW_IS(penum, TEXT_REPLACE_WIDTHS)) {
  655.     gs_point dpt;
  656.  
  657.     gs_text_replaced_width(&penum->text, penum->xy_index - 1, &dpt);
  658.     gs_distance_transform2fixed(&pgs->ctm, dpt.x, dpt.y, &penum->wxy);
  659.     } else {
  660.     double dx = 0, dy = 0;
  661.  
  662.     if (SHOW_IS_ADD_TO_SPACE(penum)) {
  663.         gs_char chr = CURRENT_CHAR(penum) & 0xff;
  664.         int fdepth = penum->fstack.depth;
  665.  
  666.         if (fdepth > 0) {
  667.         /* Add in the shifted font number. */
  668.         uint fidx = penum->fstack.items[fdepth].index;
  669.  
  670.         switch (((gs_font_type0 *) (penum->fstack.items[fdepth - 1].font))->data.FMapType) {
  671.         case fmap_1_7:
  672.         case fmap_9_7:
  673.             chr += fidx << 7;
  674.             break;
  675.         case fmap_CMap:
  676.             chr = CURRENT_CHAR(penum);  /* the full character */
  677.             if (!penum->cmap_code)
  678.             break;
  679.             /* falls through */
  680.         default:
  681.             chr += fidx << 8;
  682.         }
  683.         }
  684.         if (chr == penum->text.space.s_char) {
  685.         dx = penum->text.delta_space.x;
  686.         dy = penum->text.delta_space.y;
  687.         }
  688.     }
  689.     if (SHOW_IS_ADD_TO_ALL(penum)) {
  690.         dx += penum->text.delta_all.x;
  691.         dy += penum->text.delta_all.y;
  692.     }
  693.     if (!is_fzero2(dx, dy)) {
  694.         gs_fixed_point dxy;
  695.  
  696.         gs_distance_transform2fixed(&pgs->ctm, dx, dy, &dxy);
  697.         penum->wxy.x += dxy.x;
  698.         penum->wxy.y += dxy.y;
  699.     }
  700.     }
  701.     if (SHOW_IS_ALL_OF(penum, TEXT_DO_NONE | TEXT_INTERVENE)) {
  702.     /* HACK for cshow */
  703.     penum->continue_proc = continue_kshow;
  704.     return TEXT_PROCESS_INTERVENE;
  705.     }
  706.     /* wxy is in device coordinates */
  707.     {
  708.     int code = show_fast_move(pgs, &penum->wxy);
  709.  
  710.     if (code < 0)
  711.         return code;
  712.     }
  713.     /* Check for kerning, but not on the last character. */
  714.     if (SHOW_IS_DO_KERN(penum) && penum->index < penum->text.size) {
  715.     penum->continue_proc = continue_kshow;
  716.     return TEXT_PROCESS_INTERVENE;
  717.     }
  718.     return 0;
  719. }
  720. /* Process next character */
  721. private int
  722. show_proceed(gs_show_enum * penum)
  723. {
  724.     gs_state *pgs = penum->pgs;
  725.     gs_font *pfont;
  726.     cached_fm_pair *pair = 0;
  727.     gs_font *rfont =
  728.     (penum->fstack.depth < 0 ? pgs->font : penum->fstack.items[0].font);
  729.     int wmode = rfont->WMode;
  730.     font_proc_next_char_glyph((*next_char_glyph)) =
  731.     rfont->procs.next_char_glyph;
  732. #define get_next_char_glyph(pte, pchr, pglyph)\
  733.   (++(penum->xy_index), next_char_glyph(pte, pchr, pglyph))
  734.     gs_char chr;
  735.     gs_glyph glyph;
  736.     int code;
  737.     cached_char *cc;
  738.     gx_device *dev = gs_currentdevice_inline(pgs);
  739.     int alpha_bits = (*dev_proc(dev, get_alpha_bits)) (dev, go_text);
  740.  
  741.     if (penum->charpath_flag == cpm_show && SHOW_USES_OUTLINE(penum)) {
  742.     code = gs_state_color_load(pgs);
  743.     if (code < 0)
  744.         return code;
  745.     }
  746.   more:            /* Proceed to next character */
  747.     pfont = (penum->fstack.depth < 0 ? pgs->font :
  748.          penum->fstack.items[penum->fstack.depth].font);
  749.     penum->current_font = pfont;
  750.     /* can_cache >= 0 allows us to use cached characters, */
  751.     /* even if we can't make new cache entries. */
  752.     if (penum->can_cache >= 0) {
  753.     /* Loop with cache */
  754.     for (;;) {
  755.         switch ((code = get_next_char_glyph((gs_text_enum_t *)penum,
  756.                         &chr, &glyph))
  757.             ) {
  758.         default:    /* error */
  759.             return code;
  760.         case 2:    /* done */
  761.             return show_finish(penum);
  762.         case 1:    /* font change */
  763.             pfont = penum->fstack.items[penum->fstack.depth].font;
  764.             penum->current_font = pfont;
  765.             pgs->char_tm_valid = false;
  766.             show_state_setup(penum);
  767.             pair = 0;
  768.             /* falls through */
  769.         case 0:    /* plain char */
  770.             /*
  771.              * We don't need to set penum->current_char in the
  772.              * normal cases, but it's needed for widthshow,
  773.              * kshow, and one strange client, so we may as well
  774.              * do it here.
  775.              */
  776.             SET_CURRENT_CHAR(penum, chr);
  777.             if (glyph == gs_no_glyph) {
  778.             glyph = (*penum->encode_char)(pfont, chr,
  779.                               GLYPH_SPACE_NAME);
  780.             if (glyph == gs_no_glyph) {
  781.                 cc = 0;
  782.                 goto no_cache;
  783.             }
  784.             }
  785.             if (pair == 0)
  786.             pair = gx_lookup_fm_pair(pfont, pgs);
  787.             cc = gx_lookup_cached_char(pfont, pair, glyph, wmode,
  788.                            alpha_bits);
  789.             if (cc == 0) {
  790.             /* Character is not in cache. */
  791.             /* If possible, try for an xfont before */
  792.             /* rendering from the outline. */
  793.             if (pfont->ExactSize == fbit_use_outlines ||
  794.                 pfont->PaintType == 2
  795.                 )
  796.                 goto no_cache;
  797.             if (pfont->BitmapWidths) {
  798.                 cc = gx_lookup_xfont_char(pgs, pair, chr,
  799.                      glyph, &pfont->procs.callbacks, wmode);
  800.                 if (cc == 0)
  801.                 goto no_cache;
  802.             } else {
  803.                 if (!SHOW_USES_OUTLINE(penum) ||
  804.                 (penum->charpath_flag != cpm_show &&
  805.                  penum->charpath_flag != cpm_charwidth)
  806.                 )
  807.                 goto no_cache;
  808.                 /* We might have an xfont, but we still */
  809.                 /* want the scalable widths. */
  810.                 cc = gx_lookup_xfont_char(pgs, pair, chr,
  811.                      glyph, &pfont->procs.callbacks, wmode);
  812.                 /* Render up to the point of */
  813.                 /* setcharwidth or setcachedevice, */
  814.                 /* just as for stringwidth. */
  815.                 /* This is the only case in which we can */
  816.                 /* to go no_cache with cc != 0. */
  817.                 goto no_cache;
  818.             }
  819.             }
  820.             /* Character is in cache. */
  821.             /* We might be doing .charboxpath or stringwidth; */
  822.             /* check for these now. */
  823.             if (penum->charpath_flag == cpm_charwidth) {
  824.             /* This is charwidth.  Just move by the width. */
  825.             DO_NOTHING;
  826.             } else if (penum->charpath_flag != cpm_show) {
  827.             /* This is .charboxpath. Get the bounding box */
  828.             /* and append it to a path. */
  829.             gx_path box_path;
  830.             gs_fixed_point pt;
  831.             fixed llx, lly, urx, ury;
  832.  
  833.             code = gx_path_current_point(pgs->path, &pt);
  834.             if (code < 0)
  835.                 return code;
  836.             llx = fixed_rounded(pt.x - cc->offset.x) +
  837.                 int2fixed(penum->ftx);
  838.             lly = fixed_rounded(pt.y - cc->offset.y) +
  839.                 int2fixed(penum->fty);
  840.             urx = llx + int2fixed(cc->width),
  841.                 ury = lly + int2fixed(cc->height);
  842.             gx_path_init_local(&box_path, pgs->memory);
  843.             code =
  844.                 gx_path_add_rectangle(&box_path, llx, lly,
  845.                           urx, ury);
  846.             if (code >= 0)
  847.                 code =
  848.                 gx_path_add_char_path(pgs->show_gstate->path,
  849.                               &box_path,
  850.                               penum->charpath_flag);
  851.             if (code >= 0)
  852.                 code = gx_path_add_point(pgs->path, pt.x, pt.y);
  853.             gx_path_free(&box_path, "show_proceed(box path)");
  854.             if (code < 0)
  855.                 return code;
  856.             } else if (SHOW_IS_DRAWING(penum)) {
  857.             code = gx_image_cached_char(penum, cc);
  858.             if (code < 0)
  859.                 return code;
  860.             else if (code > 0) {
  861.                 cc = 0;
  862.                 goto no_cache;
  863.             }
  864.             }
  865.             if (SHOW_IS_SLOW(penum)) {
  866.             /* Split up the assignment so that the */
  867.             /* Watcom compiler won't reserve esi/edi. */
  868.             penum->wxy.x = cc->wxy.x;
  869.             penum->wxy.y = cc->wxy.y;
  870.             code = show_move(penum);
  871.             } else
  872.             code = show_fast_move(pgs, &cc->wxy);
  873.             if (code) {
  874.             /* Might be kshow, so store the state. */
  875.             SET_CURRENT_GLYPH(penum, glyph);
  876.             return code;
  877.             }
  878.         }
  879.     }
  880.     } else {
  881.     /* Can't use cache */
  882.     switch ((code = get_next_char_glyph((gs_text_enum_t *)penum,
  883.                         &chr, &glyph))
  884.         ) {
  885.         default:
  886.         return code;
  887.         case 2:
  888.         return show_finish(penum);
  889.         case 1:
  890.         pfont = penum->fstack.items[penum->fstack.depth].font;
  891.         penum->current_font = pfont;
  892.         show_state_setup(penum);
  893.         case 0:
  894.         ;
  895.     }
  896.     SET_CURRENT_CHAR(penum, chr);
  897.     if (glyph == gs_no_glyph) {
  898.         glyph = (*penum->encode_char)(pfont, chr, GLYPH_SPACE_NAME);
  899.     }
  900.     cc = 0;
  901.     }
  902.   no_cache:
  903.     /*
  904.      * We must call the client's rendering code.  Normally,
  905.      * we only do this if the character is not cached (cc = 0);
  906.      * however, we also must do this if we have an xfont but
  907.      * are using scalable widths.  In this case, and only this case,
  908.      * we get here with cc != 0.  penum->current_char has already
  909.      * been set, but not penum->current_glyph.
  910.      */
  911.     SET_CURRENT_GLYPH(penum, glyph);
  912.     if ((code = gs_gsave(pgs)) < 0)
  913.     return code;
  914.     /* Set the font to the current descendant font. */
  915.     pgs->font = pfont;
  916.     /* Reset the in_cachedevice flag, so that a recursive show */
  917.     /* will use the cache properly. */
  918.     pgs->in_cachedevice = CACHE_DEVICE_NONE;
  919.     /* Reset the sampling scale. */
  920.     penum->log2_scale.x = penum->log2_scale.y = 0;
  921.     /* Set the charpath data in the graphics context if necessary, */
  922.     /* so that fill and stroke will add to the path */
  923.     /* rather than having their usual effect. */
  924.     pgs->in_charpath = penum->charpath_flag;
  925.     pgs->show_gstate =
  926.     (penum->show_gstate == pgs ? pgs->saved : penum->show_gstate);
  927.     pgs->stroke_adjust = false;    /* per specification */
  928.     {
  929.     gs_fixed_point cpt;
  930.     gx_path *ppath = pgs->path;
  931.  
  932.     if ((code = gx_path_current_point_inline(ppath, &cpt)) < 0) {
  933.         /* For cshow, having no current point is acceptable. */
  934.         if (!SHOW_IS(penum, TEXT_DO_NONE))
  935.         goto rret;
  936.         cpt.x = cpt.y = 0;    /* arbitrary */
  937.     }
  938.     penum->origin.x = cpt.x;
  939.     penum->origin.y = cpt.y;
  940.     /* Normally, char_tm is valid because of show_state_setup, */
  941.     /* but if we're in a cshow, it may not be. */
  942.     gs_currentcharmatrix(pgs, NULL, true);
  943. #if 1                /*USE_FPU <= 0 */
  944.     if (pgs->ctm.txy_fixed_valid && pgs->char_tm.txy_fixed_valid) {
  945.         fixed tx = pgs->ctm.tx_fixed;
  946.         fixed ty = pgs->ctm.ty_fixed;
  947.  
  948.         gs_settocharmatrix(pgs);
  949.         cpt.x += pgs->ctm.tx_fixed - tx;
  950.         cpt.y += pgs->ctm.ty_fixed - ty;
  951.     } else
  952. #endif
  953.     {
  954.         double tx = pgs->ctm.tx;
  955.         double ty = pgs->ctm.ty;
  956.         double fpx, fpy;
  957.  
  958.         gs_settocharmatrix(pgs);
  959.         fpx = fixed2float(cpt.x) + (pgs->ctm.tx - tx);
  960.         fpy = fixed2float(cpt.y) + (pgs->ctm.ty - ty);
  961. #define f_fits_in_fixed(f) f_fits_in_bits(f, fixed_int_bits)
  962.         if (!(f_fits_in_fixed(fpx) && f_fits_in_fixed(fpy))) {
  963.         gs_note_error(code = gs_error_limitcheck);
  964.         goto rret;
  965.         }
  966.         cpt.x = float2fixed(fpx);
  967.         cpt.y = float2fixed(fpy);
  968.     }
  969.     gs_newpath(pgs);
  970.     code = show_origin_setup(pgs, cpt.x, cpt.y,
  971.                  penum->charpath_flag);
  972.     if (code < 0)
  973.         goto rret;
  974.     }
  975.     penum->width_status = sws_none;
  976.     penum->continue_proc = continue_show_update;
  977.     /* Try using the build procedure in the font. */
  978.     /* < 0 means error, 0 means success, 1 means failure. */
  979.     penum->cc = cc;        /* set this now for build procedure */
  980.     code = (*pfont->procs.build_char)((gs_text_enum_t *)penum, pgs, pfont,
  981.                       chr, glyph);
  982.     if (code < 0) {
  983.     discard(gs_note_error(code));
  984.     goto rret;
  985.     }
  986.     if (code == 0) {
  987.     code = show_update(penum);
  988.     if (code < 0)
  989.         goto rret;
  990.     /* Note that show_update does a grestore.... */
  991.     code = show_move(penum);
  992.     if (code)
  993.         return code;    /* ... so don't go to rret here. */
  994.     goto more;
  995.     }
  996.     /*
  997.      * Some BuildChar procedures do a save before the setcachedevice,
  998.      * and a restore at the end.  If we waited to allocate the cache
  999.      * device until the setcachedevice, we would attempt to free it
  1000.      * after the restore.  Therefore, allocate it now.
  1001.      */
  1002.     if (penum->dev_cache == 0) {
  1003.     code = show_cache_setup(penum);
  1004.     if (code < 0)
  1005.         goto rret;
  1006.     }
  1007.     return TEXT_PROCESS_RENDER;
  1008.     /* If we get an error while setting up for BuildChar, */
  1009.     /* we must undo the partial setup. */
  1010.   rret:gs_grestore(pgs);
  1011.     return code;
  1012. #undef get_next_char_glyph
  1013. }
  1014.  
  1015. /*
  1016.  * Prepare to retry rendering of the current character.  (This is only used
  1017.  * in one place in zchar1.c; a different approach may be better.)
  1018.  */
  1019. private int
  1020. gx_show_text_retry(gs_text_enum_t *pte)
  1021. {
  1022.     gs_show_enum *const penum = (gs_show_enum *)pte;
  1023.  
  1024.     if (penum->cc) {
  1025.     gs_font *pfont = penum->current_font;
  1026.  
  1027.     gx_free_cached_char(pfont->dir, penum->cc);
  1028.     penum->cc = 0;
  1029.     }
  1030.     gs_grestore(penum->pgs);
  1031.     penum->width_status = sws_none;
  1032.     penum->log2_scale.x = penum->log2_scale.y = 0;
  1033.     return 0;
  1034. }
  1035.  
  1036. /* Finish show or stringwidth */
  1037. private int
  1038. show_finish(gs_show_enum * penum)
  1039. {
  1040.     gs_state *pgs = penum->pgs;
  1041.     int code, rcode;
  1042.  
  1043.     if (penum->auto_release)
  1044.     penum->procs->release((gs_text_enum_t *)penum, "show_finish");
  1045.     if (!SHOW_IS_STRINGWIDTH(penum))
  1046.     return 0;
  1047.     /* Save the accumulated width before returning, */
  1048.     /* and undo the extra gsave. */
  1049.     code = gs_currentpoint(pgs, &penum->returned.total_width);
  1050.     rcode = gs_grestore(pgs);
  1051.     return (code < 0 ? code : rcode);
  1052. }
  1053.  
  1054. /* Release the structure. */
  1055. private void
  1056. gx_show_text_release(gs_text_enum_t *pte, client_name_t cname)
  1057. {
  1058.     gs_show_enum *const penum = (gs_show_enum *)pte;
  1059.  
  1060.     penum->cc = 0;
  1061.     if (penum->dev_cache2) {
  1062.     gx_device_retain((gx_device *)penum->dev_cache2, false);
  1063.     penum->dev_cache2 = 0;
  1064.     }
  1065.     if (penum->dev_cache) {
  1066.     gx_device_retain((gx_device *)penum->dev_cache, false);
  1067.     penum->dev_cache = 0;
  1068.     }
  1069.     if (penum->dev_null) {
  1070.     gx_device_retain((gx_device *)penum->dev_null, false);
  1071.     penum->dev_null = 0;
  1072.     }
  1073.     gx_default_text_release(pte, cname);
  1074. }
  1075.  
  1076. /* ------ Miscellaneous accessors ------ */
  1077.  
  1078. /* Return the charpath mode. */
  1079. gs_char_path_mode
  1080. gs_show_in_charpath(const gs_show_enum * penum)
  1081. {
  1082.     return penum->charpath_flag;
  1083. }
  1084.  
  1085. /* Return true if we only need the width from the rasterizer */
  1086. /* and can short-circuit the full rendering of the character, */
  1087. /* false if we need the actual character bits. */
  1088. /* This is only meaningful just before calling gs_setcharwidth or */
  1089. /* gs_setcachedevice[2]. */
  1090. /* Note that we can't do this if the procedure has done any extra [g]saves. */
  1091. private bool
  1092. gx_show_text_is_width_only(const gs_text_enum_t *pte)
  1093. {
  1094.     const gs_show_enum *const penum = (const gs_show_enum *)pte;
  1095.  
  1096.     /* penum->cc will be non-zero iff we are calculating */
  1097.     /* the scalable width for an xfont character. */
  1098.     return ((!SHOW_USES_OUTLINE(penum) || penum->cc != 0) &&
  1099.         penum->pgs->level == penum->level + 1);
  1100. }
  1101.  
  1102. /* Return the width of the just-enumerated character (for cshow). */
  1103. private int
  1104. gx_show_text_current_width(const gs_text_enum_t *pte, gs_point *pwidth)
  1105. {
  1106.     const gs_show_enum *const penum = (const gs_show_enum *)pte;
  1107.  
  1108.     return gs_idtransform(penum->pgs,
  1109.               fixed2float(penum->wxy.x),
  1110.               fixed2float(penum->wxy.y), pwidth);
  1111. }
  1112.  
  1113. /* Return the current font for cshow. */
  1114. gs_font *
  1115. gs_show_current_font(const gs_show_enum * penum)
  1116. {
  1117.     return (penum->fstack.depth < 0 ? penum->pgs->font :
  1118.         penum->fstack.items[penum->fstack.depth].font);
  1119. }
  1120.  
  1121. /* ------ Internal routines ------ */
  1122.  
  1123. /* Initialize the gstate-derived parts of a show enumerator. */
  1124. /* We do this both when starting the show operation, */
  1125. /* and when returning from the kshow callout. */
  1126. /* Uses only penum->pgs, penum->fstack. */
  1127. private int
  1128. show_state_setup(gs_show_enum * penum)
  1129. {
  1130.     gs_state *pgs = penum->pgs;
  1131.     gx_clip_path *pcpath;
  1132.     gs_font *pfont;
  1133.  
  1134.     if (penum->fstack.depth <= 0) {
  1135.     pfont = pgs->font;
  1136.     gs_currentcharmatrix(pgs, NULL, 1);    /* make char_tm valid */
  1137.     } else {
  1138.     /* We have to concatenate the parent's FontMatrix as well. */
  1139.     gs_matrix mat;
  1140.     const gx_font_stack_item_t *pfsi =
  1141.         &penum->fstack.items[penum->fstack.depth];
  1142.  
  1143.     pfont = pfsi->font;
  1144.     gs_matrix_multiply(&pfont->FontMatrix,
  1145.                &pfsi[-1].font->FontMatrix, &mat);
  1146.     gs_setcharmatrix(pgs, &mat);
  1147.     }
  1148.     penum->current_font = pfont;
  1149.     /* Skewing or non-rectangular rotation are not supported. */
  1150.     if (!CACHE_ROTATED_CHARS &&
  1151.     (is_fzero2(pgs->char_tm.xy, pgs->char_tm.yx) ||
  1152.      is_fzero2(pgs->char_tm.xx, pgs->char_tm.yy))
  1153.     )
  1154.     penum->can_cache = 0;
  1155.     if (penum->can_cache >= 0 &&
  1156.     gx_effective_clip_path(pgs, &pcpath) >= 0
  1157.     ) {
  1158.     gs_fixed_rect cbox;
  1159.  
  1160.     gx_cpath_inner_box(pcpath, &cbox);
  1161.     /* Since characters occupy an integral number of pixels, */
  1162.     /* we can (and should) round the inner clipping box */
  1163.     /* outward rather than inward. */
  1164.     penum->ibox.p.x = fixed2int_var(cbox.p.x);
  1165.     penum->ibox.p.y = fixed2int_var(cbox.p.y);
  1166.     penum->ibox.q.x = fixed2int_var_ceiling(cbox.q.x);
  1167.     penum->ibox.q.y = fixed2int_var_ceiling(cbox.q.y);
  1168.     gx_cpath_outer_box(pcpath, &cbox);
  1169.     penum->obox.p.x = fixed2int_var(cbox.p.x);
  1170.     penum->obox.p.y = fixed2int_var(cbox.p.y);
  1171.     penum->obox.q.x = fixed2int_var_ceiling(cbox.q.x);
  1172.     penum->obox.q.y = fixed2int_var_ceiling(cbox.q.y);
  1173. #if 1                /*USE_FPU <= 0 */
  1174.     if (pgs->ctm.txy_fixed_valid && pgs->char_tm.txy_fixed_valid) {
  1175.         penum->ftx = (int)fixed2long(pgs->char_tm.tx_fixed -
  1176.                      pgs->ctm.tx_fixed);
  1177.         penum->fty = (int)fixed2long(pgs->char_tm.ty_fixed -
  1178.                      pgs->ctm.ty_fixed);
  1179.     } else {
  1180. #endif
  1181.         double fdx = pgs->char_tm.tx - pgs->ctm.tx;
  1182.         double fdy = pgs->char_tm.ty - pgs->ctm.ty;
  1183.  
  1184. #define int_bits (arch_sizeof_int * 8 - 1)
  1185.         if (!(f_fits_in_bits(fdx, int_bits) &&
  1186.           f_fits_in_bits(fdy, int_bits))
  1187.         )
  1188.         return_error(gs_error_limitcheck);
  1189. #undef int_bits
  1190.         penum->ftx = (int)fdx;
  1191.         penum->fty = (int)fdy;
  1192.     }
  1193.     }
  1194.     show_set_encode_char(penum);
  1195.     return 0;
  1196. }
  1197.  
  1198. /* Set the suggested oversampling scale for character rendering. */
  1199. private void
  1200. show_set_scale(gs_show_enum * penum)
  1201. {
  1202.     /*
  1203.      * Decide whether to oversample.
  1204.      * We have to decide this each time setcachedevice is called.
  1205.      */
  1206.     const gs_state *pgs = penum->pgs;
  1207.  
  1208.     if ((penum->charpath_flag == cpm_show ||
  1209.      penum->charpath_flag == cpm_charwidth) &&
  1210.     SHOW_USES_OUTLINE(penum) &&
  1211.     gx_path_is_void_inline(pgs->path) &&
  1212.     /* Oversampling rotated characters doesn't work well. */
  1213.     (is_fzero2(pgs->char_tm.xy, pgs->char_tm.yx) ||
  1214.      is_fzero2(pgs->char_tm.xx, pgs->char_tm.yy))
  1215.     ) {
  1216.     const gs_font_base *pfont = (gs_font_base *) pgs->font;
  1217.     gs_fixed_point extent;
  1218.     int code = gs_distance_transform2fixed(&pgs->char_tm,
  1219.                   pfont->FontBBox.q.x - pfont->FontBBox.p.x,
  1220.                   pfont->FontBBox.q.y - pfont->FontBBox.p.y,
  1221.                            &extent);
  1222.  
  1223.     if (code >= 0) {
  1224.         int sx =
  1225.         (extent.x == 0 ? 0 :
  1226.          any_abs(extent.x) < int2fixed(25) ? 2 :
  1227.          any_abs(extent.x) < int2fixed(60) ? 1 :
  1228.          0);
  1229.         int sy =
  1230.         (extent.y == 0 ? 0 :
  1231.          any_abs(extent.y) < int2fixed(25) ? 2 :
  1232.          any_abs(extent.y) < int2fixed(60) ? 1 :
  1233.          0);
  1234.  
  1235.         /* If we oversample at all, make sure we do it */
  1236.         /* in both X and Y. */
  1237.         if (sx == 0 && sy != 0)
  1238.         sx = 1;
  1239.         else if (sy == 0 && sx != 0)
  1240.         sy = 1;
  1241.         penum->log2_suggested_scale.x = sx;
  1242.         penum->log2_suggested_scale.y = sy;
  1243.         return;
  1244.     }
  1245.     }
  1246.     /* By default, don't scale. */
  1247.     penum->log2_suggested_scale.x =
  1248.     penum->log2_suggested_scale.y = 0;
  1249. }
  1250.  
  1251. /* Set up the cache device and related information. */
  1252. /* Note that we always allocate both cache devices, */
  1253. /* even if we only use one of them. */
  1254. private int
  1255. show_cache_setup(gs_show_enum * penum)
  1256. {
  1257.     gs_state *pgs = penum->pgs;
  1258.     gs_memory_t *mem = pgs->memory;
  1259.     gx_device_memory *dev =
  1260.     gs_alloc_struct(mem, gx_device_memory, &st_device_memory,
  1261.             "show_cache_setup(dev_cache)");
  1262.     gx_device_memory *dev2 =
  1263.     gs_alloc_struct(mem, gx_device_memory, &st_device_memory,
  1264.             "show_cache_setup(dev_cache2)");
  1265.  
  1266.     if (dev == 0 || dev2 == 0) {
  1267.     gs_free_object(mem, dev2, "show_cache_setup(dev_cache2)");
  1268.     gs_free_object(mem, dev, "show_cache_setup(dev_cache)");
  1269.     return_error(gs_error_VMerror);
  1270.     }
  1271.     /*
  1272.      * We only initialize the devices for the sake of the GC,
  1273.      * (since we have to re-initialize dev as either a mem_mono
  1274.      * or a mem_abuf device before actually using it) and also
  1275.      * to set its memory pointer.
  1276.      */
  1277.     gs_make_mem_mono_device(dev, mem, gs_currentdevice_inline(pgs));
  1278.     penum->dev_cache = dev;
  1279.     gs_make_mem_mono_device(dev2, mem, gs_currentdevice_inline(pgs));
  1280.     penum->dev_cache2 = dev2;
  1281.     /* Retain these devices, since they are referenced from the enumerator. */
  1282.     gx_device_retain((gx_device *)dev, true);
  1283.     gx_device_retain((gx_device *)dev2, true);
  1284.     return 0;
  1285. }
  1286.  
  1287. /* Set the character origin as the origin of the coordinate system. */
  1288. /* Used before rendering characters, and for moving the origin */
  1289. /* in setcachedevice2 when WMode=1. */
  1290. private int
  1291. show_origin_setup(gs_state * pgs, fixed cpt_x, fixed cpt_y,
  1292.           gs_char_path_mode charpath_flag)
  1293. {
  1294.     if (charpath_flag == cpm_show) {
  1295.     /* Round the translation in the graphics state. */
  1296.     /* This helps prevent rounding artifacts later. */
  1297.     cpt_x = fixed_rounded(cpt_x);
  1298.     cpt_y = fixed_rounded(cpt_y);
  1299.     }
  1300.     /*
  1301.      * BuildChar procedures expect the current point to be undefined,
  1302.      * so we omit the gx_path_add_point with ctm.t*_fixed.
  1303.      */
  1304.     return gx_translate_to_fixed(pgs, cpt_x, cpt_y);
  1305. }
  1306.